changeBet = function(lien) {
  if (lien == null)
    lien = "gestion-paris.htm";
  var vParentId = $("input[name=parentId]").val();
  $.post(lien, {}, function(xml) {
    $("#corps").html(xml);
    parseBet();
  });
}

removeBets = function() {
  var ids = "";
  var pgs = "";
  $("input[name=check]:checked").each(function() {
    ids += ";" + $(this).val();
    pgs += ", " + $(this).parent('td').next('td').text();
  });
  if (ids == "") {
    alert("Vous devez sélectionner au moins un pari pour pouvoir le supprimer.");
  } else if (confirm("Êtes vous sûr de vouloir supprimer les paris :\n" + pgs.substring(2))) {
    ids = ids.substring(1);
    $.post("removebets.htm", {
      pids:ids
    }, changeBet);
  }
}

showGamers = function() {
  upOrDownSubmenu($("#submenu li:nth-child(2) a"), "show-gamers.htm", msgNoBet, msgTooManyBet);
}

addBet = function() {
  var pid = $("input[name=parentId]").val();
  var name = $("input[name=betName]").val();
  if (name == "") {
    alert("Vous devez entrer un nom pour la pari.");
  } else {
    var input = document.createElement("input");
    $(input).attr("type", "checkbox");
    $(input).attr("name", "check");
    $(input).attr("value", "0");
    $(input).attr("checked", "checked");
    $(input).click(function() {
      checkLine($(this));
    });

    var td1 = document.createElement("td");
    $(td1).append(input);

    var td2 = document.createElement("td");
    $(td2).append(name);

    var section = $("#tree-" + pid + " span").text();
    var td3 = document.createElement("td");
    $(td3).append(section);

    var now = Date.now();
    var td4 = document.createElement("td");
    $(td4).append(now);

    var start = now;
    var td5 = document.createElement("td");
    $(td5).append(start);

    var end = now;
    var td6 = document.createElement("td");
    $(td6).append(end);

    var image = document.createElement("img");
    $(image).attr("alt", "");
    $(image).attr("title","Inactif");
    $(image).attr("src", "/img/adm/light.png");

    var div = document.createElement("div");
    $(div).addClass("light");
    $(div).addClass("red");
    $(div).append(image);

    var td7 = document.createElement("td");
    $(td7).append(div);

    var tr = document.createElement("tr");
    $(tr).append(td1);
    $(tr).append(td2);
    $(tr).append(td3);
    $(tr).append(td4);
    $(tr).append(td5);
    $(tr).append(td6);
    $(tr).append(td7);
    $(tr).addClass("current");

    $("#list-of-bets tbody").append(tr);
    $("input[name=betName]").val("");
    upOrDownSubmenuNewBet($("#submenu li:nth-child(1) a"), pid, name, function() {
      $("#submit-bet-save").click(saveBet);
      $("#submit-betchoice-edit").click(saveChoice);
    });
  }
}

editBet = function() {
  upOrDownSubmenu($("#submenu li:nth-child(1) a"), "editbet.htm", msgNoBet, msgTooManyBet, function() {
    $("#submit-bet-save").click(saveBet);
    $("#submit-betchoice-edit").click(saveChoice);
    $("#terminate-bet").click(setWinningChoice);
    $("#edit-bet a[href=#supprimer").click(deleteChoice);
  });
}

newChoice = function() {
  upOrDownSubmenu($("#submenu li:nth-child(1) a"), "editbet.htm", msgNoBet, msgTooManyBet, function() {
    $("#submit-bet-save").click(saveBet);
    $("#submit-betchoice-edit").click(saveChoice);
  });
}

deleteChoice = function() {
  alert("Fonctionnalité non disponible");
}

saveChoice = function() {
  var choix =  $("input[name=choiceName]").val();
  var name = $("input[name=name]").val();
  var id = $("#edit-bet input[name=id]").val();
  if (choix == "") {
    alert("Vous devez sélectionner un nom pour le choix.");
  } else {
    $.post("newchoice.htm", {
      pid:id,
      pchoix:choix
    }, function(xml){
      var r = $(xml).find("result").text() == "1";
      if (!r) {
        alert("Impossible d'ajouter ce choix.");
      } else {
        var label = document.createElement("label");
          $(label).text(choix);
          
        var a = document.createElement("a");
          $(a).attr('href', '#supprimer');
          $(a).text('Supprimer');
          $(a).click(deleteChoice);

        var li = document.createElement("li");
          $(li).append(label);
          $(li).append(a);

        $("ul#choices-available").append(li);
      }
    });
  }
}

saveBet = function() {
  var id = $("#edit-bet input[name=id]").val();
  var name = $("input[name=name]").val();
  var desc = $("textarea[name=description]").val();
  var status = $("input[name=status]").is(":checked");
  var section = $("select[name=subsection]").val();
  var start = $("input[name=startTime]").val();
  var end = $("input[name=endTime]").val();
  $.post("saveOrUdpateBet.htm", {
    pid:id,
    pname:name,
    pdesc:desc,
    pstatus:status,
    psection:section,
    pstart:start,
    pend:end
  }, function(xml){
    var r = $(xml).find("result").text() == "1";
    if (!r) {
      alert("Impossible d'enregistrer les modifications.");
    } else {
      alert('Modification effectuée !');
      changeBet();
    }
  });
}

setWinningChoice = function() {
  var choiceid = $("input[name=winner]:checked").val();
  $.post("setWinningChoice.htm", { pchid:choiceid }, function(xml){
    var r = $(xml).find("result").text() == "1";
    if (!r) {
      alert("Impossible de clôturer le pari.");
    } else {
      alert('Modification effectuée !');
      changeBet();
    }
  });
}


upOrDownSubmenuNewBet = function(li, pid, name, callback) {
  var uid = -1;
  if ($("input[name=check]:checked").length == 1) {
    uid = $("input[name=check]:checked").val();
  }
  if (!$(li).is('.current')) {
    var ntop = topMenuTop + topMenuHeight + 1 + 'px';
    $("#submenu a").removeClass('current');
    $(li).addClass('current');
    $.post("newbet.htm", {
      pname:name,
      psection:pid
    }, function(xml) {
      $("#area-content").html(xml);
      $("#area").animate( {
        top:ntop
      }, 'normal');
      if (typeof callback === 'function')
        callback();
    });
  }
}

checkBet = function() {
  $(this).parent().prev().children(':checkbox').attr('checked','checked');
  editBet();
}

parseBet = function() {
  /*	$("input[name=pageName]").keydown(function(e){ if(e.keyCode == 13) { addPage(); return false; }; });*/
  $("#list-of-bets thead input:checkbox").click(function(){
    checkAllLines("list-of-bets");
  });
  $("#list-of-bets tbody input:checkbox").click(function(){
    checkLine($(this));
  });
  $("a[href=#remove]").click(removeBets);
  $("a[href=#beti]").click(checkBet);
  $("#submit-new-bet").click(addBet);
  fillSubMenu([
    ["Éditer un pari",editBet],
    ["Afficher les joueurs", showGamers]
    ]);
}